package com.ruoyi.basic.service.impl;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.ImageUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.basic.mapper.BasicImageMapper;
import com.ruoyi.basic.domain.BasicImage;
import com.ruoyi.basic.service.IBasicImageService;
import com.ruoyi.common.core.text.Convert;

/**
 * 员工图片Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class BasicImageServiceImpl extends ServiceImpl<BasicImageMapper, BasicImage> implements IBasicImageService
{
    /**
     * 查询员工图片
     * 
     * @param imageId 员工图片主键
     * @return 员工图片
     */
    @Override
    public BasicImage selectBasicImageByImageId(Long imageId)
    {
        return baseMapper.selectBasicImageByImageId(imageId);
    }

    /**
     * 查询员工图片列表
     * 
     * @param basicImage 员工图片
     * @return 员工图片
     */
    @Override
    public List<BasicImage> selectBasicImageList(BasicImage basicImage)
    {
        List<BasicImage> imageList = baseMapper.selectBasicImageList(basicImage);
        for(BasicImage image : imageList){
            String filePath = RuoYiConfig.getProfile() + StringUtils.substringAfter(image.getImageFile(), Constants.RESOURCE_PREFIX);
            File file = new File(filePath);
            if(file.exists()) {
                String imageContent = ImageUtils.imageToBase64(filePath);
                if (StringUtils.isNotEmpty(imageContent)) {
                    imageContent = "data:image/files;base64," + imageContent;
                    image.setImageContent(imageContent);
                }
            }
        }
        return imageList;
    }

    /**
     * 查询员工图片列表
     *
     * @param userId 员工id
     * @return 员工图片
     */
    @Override
    public List<BasicImage> selectBasicImageListByUserId(Long userId)
    {
        return baseMapper.selectBasicImageByUserId(userId);
    }

    /**
     * 查询员工图片列表
     *
     * @param imageIds 图片id
     * @return 员工图片
     */
    @Override
    public List<BasicImage> selectBasicImageListByImageIds(String imageIds)
    {
        return baseMapper.selectBasicImageByImageIds(Convert.toLongArray(imageIds));
    }

    /**
     * 新增员工图片
     * 
     * @param basicImage 员工图片
     * @return 结果
     */
    @Override
    public int insertBasicImage(BasicImage basicImage)
    {
        basicImage.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(basicImage);
    }

    /**
     * 修改员工图片
     * 
     * @param basicImage 员工图片
     * @return 结果
     */
    @Override
    public int updateBasicImage(BasicImage basicImage)
    {
        basicImage.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(basicImage);
    }

    /**
     * 批量删除员工图片
     * 
     * @param imageIds 需要删除的员工图片主键
     * @return 结果
     */
    @Override
    public int deleteBasicImageByImageIds(String imageIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(imageIds)));
    }

    /**
     * 删除员工图片信息
     * 
     * @param imageId 员工图片主键
     * @return 结果
     */
    @Override
    public int deleteBasicImageByImageId(Long imageId)
    {
        return baseMapper.deleteById(imageId);
    }

    /**
     * 根据文件名查询文件是否存在
     * @param fileName
     * @param basicImage
     * @return
     */
    @Override
    public boolean checkBasicImageByFileName(String fileName, BasicImage basicImage) {
        LambdaQueryWrapper<BasicImage> queryWrapper = new LambdaQueryWrapper<BasicImage>();
        queryWrapper.like(BasicImage::getImageFile, fileName);
        queryWrapper.eq(BasicImage::getUserId, basicImage.getUserId());
        if(StringUtils.isNotNull(basicImage.getImageId())) {
            queryWrapper.ne(BasicImage::getImageId, basicImage.getImageId());
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count > 0) {
            return true;
        }else{
            return false;
        }
    }
}
